﻿using System;
using System.Collections.Generic;
using System.Text;

namespace havefun
{
    public class Vector3
    {
        public Vector3(float fx, float fy, float fz)
        {
            x = fx;
            y = fy;
            z = fz;
        }
        public float x;
        public float y;
        public float z;
        public static Vector3 operator -(Vector3 l, Vector3 r)
        {
            return new Vector3(l.x - r.x, l.y - r.y, l.z - r.z);
        }
        public float Dot(Vector3 v)//求点积
        {
            return x * v.x + y * v.y + z * v.z;
        }
        public Vector3 Cross(Vector3 v)//求差积
        {
            return new Vector3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
        }
        static bool SameSide(Vector3 A, Vector3 B, Vector3 C, Vector3 P)//判断点P是否在边AB的C一侧
        {
            Vector3 AB = B - A;
            Vector3 AC = C - A;
            Vector3 AP = P - A;
            Vector3 v1 = AB.Cross(AC);
            Vector3 v2 = AB.Cross(AP);
            return v1.Dot(v2) >= 0;
        }
        public static bool PointinTriangle(Vector3 A, Vector3 B, Vector3 C, Vector3 P)//判断点P是否在三角形ABC中
        {
            return SameSide(A, B, C, P) && SameSide(B, C, A, P) && SameSide(C, A, B, P);
        }
    };

}
